using Avalonia.Controls;
using Kitware.AvaloniaControls;
using Kitware.VTK;
namespace AvaloniaDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            VTKControl.AttachedToVisualTree += VTKControl_AttachedToVisualTree;

            this.Loaded += MainWindow_Loaded;
            
        }

        private void MainWindow_Loaded(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
        {
        }

        private void VTKControl_AttachedToVisualTree(object? sender, Avalonia.VisualTreeAttachmentEventArgs e)
        {

            Kitware.AvaloniaControls.RenderWindowControl? mainView = sender as Kitware.AvaloniaControls.RenderWindowControl;
            vtkRenderer renderer = vtkRenderer.New();
            renderer.SetBackgroundAlpha(1.0);
            mainView.RenderWindow.AddRenderer(renderer);
            vtkInteractorStyleTrackballCamera interactorStyle = vtkInteractorStyleTrackballCamera.New();
            mainView.RenderWindow.GetInteractor().SetInteractorStyle(interactorStyle);

            // Demonstrate how to use the vtkBoxWidget 3D widget,
            // This script uses a 3D box widget to define a "clipping box" to clip some
            // simple geometry (a mace). Make sure that you hit the "W" key to activate the widget.
            // Create a mace out of filters.
            sphere = vtkSphereSource.New();
            cone = vtkConeSource.New();
            glyph = vtkGlyph3D.New();

            glyph.SetInputConnection(sphere.GetOutputPort());
            glyph.SetSourceConnection(cone.GetOutputPort());
            glyph.SetVectorModeToUseNormal();
            glyph.SetScaleModeToScaleByVector();
            glyph.SetScaleFactor(0.25);

            // The sphere and spikes are appended into a single polydata. This just makes things
            // simpler to manage.
            apd = vtkAppendPolyData.New();
            apd.AddInputConnection(glyph.GetOutputPort());
            apd.AddInputConnection(sphere.GetOutputPort());

            maceMapper = vtkPolyDataMapper.New();
            maceMapper.SetInputConnection(apd.GetOutputPort());

            maceActor = vtkLODActor.New();
            maceActor.SetMapper(maceMapper);
            maceActor.VisibilityOn();

            // This portion of the code clips the mace with the vtkPlanes implicit function.
            // The clipped region is colored green.
            planes = vtkPlanes.New();
            clipper = vtkClipPolyData.New();

            clipper.SetInputConnection(apd.GetOutputPort());
            clipper.SetClipFunction(planes);
            clipper.InsideOutOn();

            selectMapper = vtkPolyDataMapper.New();
            selectMapper.SetInputConnection(clipper.GetOutputPort());

            selectActor = vtkLODActor.New();
            selectActor.SetMapper(selectMapper);
            selectActor.GetProperty().SetColor(0, 1, 0);
            selectActor.VisibilityOff();
            selectActor.SetScale(1.01, 1.01, 1.01);

            renderer.AddActor(maceActor);
            renderer.AddActor(selectActor);
        }

        static vtkSphereSource sphere;
        static vtkConeSource cone;
        static vtkGlyph3D glyph;
        static vtkAppendPolyData apd;
        static vtkPolyDataMapper maceMapper;
        static vtkLODActor maceActor;
        static vtkPlanes planes;
        static vtkClipPolyData clipper;
        static vtkPolyDataMapper selectMapper;
        static vtkLODActor selectActor;

    }
}